package com.ruoyi.web.controller.news;

import com.github.pagehelper.PageInfo;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.bean.BeanUtils;
import com.ruoyi.system.domain.news.NewsStudyTrain;
import com.ruoyi.system.domain.news.dto.NewsStudyTrainDto;
import com.ruoyi.system.domain.news.query.NewsStudyTrainQuery;
import com.ruoyi.system.domain.news.vo.NewsStudyTrainVo;
import com.ruoyi.system.service.news.NewsStudyTrainService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/news/newsStudyTrain")
@Api(value = "newsStudyTrainController", tags  = "学习培训")
public class NewsStudyTrainController extends BaseController {

	@Resource
	private NewsStudyTrainService newsStudyTrainService;

    @ApiOperation(value = "新增/修改学习培训信息", notes = "新增/修改学习培训信息")
	@PostMapping("/submit")
	public R submit(@RequestBody NewsStudyTrainDto newsStudyTrainDto) {
		LoginUser loginUser = SecurityUtils.getLoginUser();
		NewsStudyTrain newsStudyTrain = new NewsStudyTrain();
		BeanUtils.copyProperties(newsStudyTrainDto,newsStudyTrain);
		if (newsStudyTrain.getId()==null) {
			newsStudyTrain.setCreateBy(loginUser.getUsername());
			newsStudyTrain.setDeptId(loginUser.getDeptId());
			newsStudyTrain.setCreateTime(new Date());
			newsStudyTrain.setUpdateBy(loginUser.getUsername());
			newsStudyTrain.setUpdateTime(new Date());
		} else {
			newsStudyTrain.setUpdateBy(loginUser.getUsername());
			newsStudyTrain.setUpdateTime(new Date());
		}
		boolean result = newsStudyTrainService.saveOrUpdate(newsStudyTrain);
		if (result) {
			return R.ok();
		} else {
			return R.fail("操作失败");
		}
	}

	@ApiOperation(value = "删除学习培训信息", notes = "删除学习培训信息")
	@DeleteMapping("/delete")
	public R delete(@RequestParam(value = "id") Long id) {
		boolean result = newsStudyTrainService.removeById(id);
		if (result) {
			return R.ok();
		} else {
			return R.fail("操作失败");
		}
	}

	@ApiOperation(value = "获取单个学习培训信息", notes = "获取单个学习培训信息")
	@GetMapping("/getById")
	public R<NewsStudyTrainVo> getById(@RequestParam(value = "id") Long id) {
		NewsStudyTrainQuery newsStudyTrainQuery = new NewsStudyTrainQuery();
		newsStudyTrainQuery.setId(id);
		List<NewsStudyTrainVo> newsStudyTrainList = newsStudyTrainService.getNewsStudyTrainList(newsStudyTrainQuery);
		if (newsStudyTrainList!=null&&newsStudyTrainList.size()>0) {
			return R.ok(newsStudyTrainList.get(0));
		} else {
			return R.fail("数据不存在");
		}
	}

	@ApiOperation(value = "获取学习培训信息列表")
	@PostMapping(value = "/list")
	public R<PageInfo<NewsStudyTrainVo>> list(@RequestBody NewsStudyTrainQuery newsStudyTrainQuery) {
		List<NewsStudyTrainVo> newsStudyTrainList = newsStudyTrainService.getNewsStudyTrainList(newsStudyTrainQuery);
		return R.ok(new PageInfo<>(newsStudyTrainList));
	}
}
